{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 什么是字典？\n",
    "+ 字典以一种联系的方式储存信息，字典储存的是键值对（*\"key-value\" pairs)* ，其中任意一条信息至少和另外一条信息相联系。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'key_1' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-659a36a44065>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m dictionary_name = {key_1: value_1,\n\u001b[0m\u001b[0;32m      2\u001b[0m                    \u001b[0mkey_2\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mvalue_2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m                    \u001b[0mkey_3\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mvalue_3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m                    }\n",
      "\u001b[1;31mNameError\u001b[0m: name 'key_1' is not defined"
     ]
    }
   ],
   "source": [
    "dictionary_name = {key_1: value_1,\n",
    "                   key_2: value_2,\n",
    "                   key_3: value_3,\n",
    "                   }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Word: list\n",
      "Meaning: A collection of values that are not connected, but have an order.\n",
      "\n",
      "Word: dictionary\n",
      "Meaning: A collection of key-value pairs.\n",
      "\n",
      "Word: function\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n",
      "<built-in method items of dict object at 0x000002E0AEE40AB0>\n",
      "\n",
      "Word: list\n",
      "Meaning: A collection of values that are not connected, but have an order.\n",
      "<built-in method items of dict object at 0x000002E0AEE40AB0>\n",
      "\n",
      "Word: dictionary\n",
      "Meaning: A collection of key-value pairs.\n",
      "<built-in method items of dict object at 0x000002E0AEE40AB0>\n",
      "\n",
      "Word: function\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n"
     ]
    }
   ],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "print(\"\\nWord: %s\" % 'list')\n",
    "print(\"Meaning: %s\" % python_words['list'])\n",
    "      \n",
    "print(\"\\nWord: %s\" % 'dictionary')\n",
    "print(\"Meaning: %s\" % python_words['dictionary'])\n",
    "\n",
    "print(\"\\nWord: %s\" % 'function')\n",
    "print(\"Meaning: %s\" % python_words['function'])\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 字典有自己的循环形式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "for key_name, value_name in dictionary_name.items():\n",
    "    print(key_name) # The key is stored in whatever you called the first variable.\n",
    "    print(value_name) # The value associated with that key is stored in your second variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "韩梅梅\n",
      "李雷\n",
      "林涛\n",
      "Jim\n",
      "Kate\n",
      "Lucy\n",
      "我想0排的韩梅梅\n",
      "我想1排的李雷\n",
      "我想2排的林涛\n",
      "我想3排的Jim\n",
      "我想4排的Kate\n",
      "我想5排的Lucy\n"
     ]
    }
   ],
   "source": [
    "# 比较一下list\n",
    "\n",
    "students = ['韩梅梅', '李雷', '林涛','Jim','Kate','Lucy']\n",
    "\n",
    "for student in students:\n",
    "    print(student)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 字典中的信息的储存不按顺序，也不能按储存的顺序来提取，换言之，没有index。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-13-c1a766fc7103>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-13-c1a766fc7103>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    python_word{0}\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "python_word{0}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我想0排的韩梅梅\n",
      "我想1排的李雷\n",
      "我想2排的林涛\n",
      "我想3排的Jim\n",
      "我想4排的Kate\n",
      "我想5排的Lucy\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'韩梅梅'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for index, student in enumerate(students):\n",
    "    place = str(index)\n",
    "    print('我想' + place + '排的' + student)\n",
    "students[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**练习**  \n",
    "现在你准备一个问题，问你身边的三个同学。  \n",
    "把这三个同学的姓名或昵称和他们对你问题的回答以dict的形式储存起来。  \n",
    "再以循环的形式print出来。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一般的字典操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 增加key-value pair"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Word: list\n",
      "Meaning: A collection of values that are not connected, but have an order.\n",
      "\n",
      "Word: dictionary\n",
      "Meaning: A collection of key-value pairs.\n",
      "\n",
      "Word: function\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n"
     ]
    }
   ],
   "source": [
    "python_words = {}\n",
    "\n",
    "# Fill the dictionary, pair by pair.\n",
    "python_words['list'] ='A collection of values that are not connected, but have an order.'\n",
    "python_words['dictionary'] = 'A collection of key-value pairs.'\n",
    "python_words['function'] = 'A named set of instructions that defines a set of actions in Python.'\n",
    "\n",
    "# Print out the items in the dictionary.\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 改变key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Word: lisst\n",
      "Meaning: A collection of values that are not connected, but have an order.\n",
      "\n",
      "Word: dictionary\n",
      "Meaning: A collection of key-value pairs.\n",
      "\n",
      "Word: function\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n",
      "\n",
      "Word: list\n",
      "Meaning: A collection of values that are not connected, but have an order.\n",
      "\n",
      "Word: dictionary\n",
      "Meaning: A collection of key-value pairs.\n",
      "\n",
      "Word: function\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n",
      "\n",
      "Word: list\n",
      "Meaning: A collection of values that are not connected, but have an order.\n"
     ]
    }
   ],
   "source": [
    "python_words = {'lisst': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "# Create a new, correct key, and connect it to the old value.\n",
    "#  Then delete the old key.\n",
    "python_words['list'] = python_words['lisst']\n",
    "\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)\n",
    "\n",
    "del python_words['lisst']\n",
    "\n",
    "# Print the dictionary, to show that the key has changed.\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 改变value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dictionary: A collection of key-value pairs.\n",
      "\n",
      "dictionary: A collection of key-value pairs. Each key can be used to access its corresponding value.\n"
     ]
    }
   ],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "print('dictionary: ' + python_words['dictionary'])\n",
    "    \n",
    "# Clarify one of the meanings.\n",
    "python_words['dictionary'] = 'A collection of key-value pairs. Each key can be used to access its corresponding value.'\n",
    "\n",
    "print('\\ndictionary: ' + python_words['dictionary'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 删除key-value pair"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "These are the Python words I know:\n",
      "\n",
      "Word: list\n",
      "Meaning: A collection of values that are not connected, but have an order.\n",
      "\n",
      "Word: dictionary\n",
      "Meaning: A collection of key-value pairs.\n",
      "\n",
      "Word: function\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n",
      "\n",
      "\n",
      "These are the Python words I know:\n",
      "\n",
      "Word: dictionary\n",
      "Meaning: A collection of key-value pairs.\n",
      "\n",
      "Word: function\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n"
     ]
    }
   ],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "# Show the current set of words and meanings.\n",
    "print(\"\\n\\nThese are the Python words I know:\")\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)\n",
    "    \n",
    "# Remove the word 'list' and its meaning.\n",
    "del python_words['list']\n",
    "\n",
    "# Show the current set of words and meanings.\n",
    "print(\"\\n\\nThese are the Python words I know:\")\n",
    "for word, meaning in python_words.items():\n",
    "    print(\"\\nWord: %s\" % word)\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "These are the Python words I know:\n",
      "\n",
      "list: A collection of values that are not connected, but have an order.\n",
      "\n",
      "dictionary: A collection of key-value pairs.\n",
      "\n",
      "function: A named set of instructions that defines a set of actions in Python.\n",
      "\n",
      "\n",
      "These are the Python words I know:\n",
      "\n",
      "dictionary: A collection of key-value pairs.\n",
      "\n",
      "function: A named set of instructions that defines a set of actions in Python.\n"
     ]
    }
   ],
   "source": [
    "# 和函数结合使用\n",
    "def show_words_meanings(python_words):\n",
    "    # This function takes in a dictionary of python words and meanings,\n",
    "    #  and prints out each word with its meaning.\n",
    "    print(\"\\n\\nThese are the Python words I know:\")\n",
    "    for word, meaning in python_words.items():\n",
    "        print(\"\\n%s: %s\" % (word, meaning))\n",
    "        \n",
    "\n",
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "show_words_meanings(python_words)\n",
    "    \n",
    "# Remove the word 'list' and its meaning.\n",
    "del python_words['list']\n",
    "\n",
    "show_words_meanings(python_words)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**练习**\n",
    "\n",
    "已知，以下食物每100g的卡路里如下：\n",
    "\n",
    "小米粥 45\n",
    "粗粮馒头 223\n",
    "全麦面包 235\n",
    "瘦猪肉 143\n",
    "鸡翅 194\n",
    "培根 181\n",
    "火腿肠 212\n",
    "\n",
    "\n",
    "建立一个空的字典，把上述键值对依此添加进去。\n",
    "通过循环形式，显示，如“我吃了二两”+“小米粥”，“增加了”+“45”千卡路里。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 遍历字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 遍历字典里所有的key-value paris\n",
    "+ 以.term()函数把键值对变成了元组构成的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Key: key_1\n",
      "Value: value_1\n",
      "\n",
      "Key: key_2\n",
      "Value: value_2\n",
      "\n",
      "Key: key_3\n",
      "Value: value_3\n"
     ]
    }
   ],
   "source": [
    "my_dict = {'key_1': 'value_1',\n",
    "    'key_2': 'value_2',\n",
    "    'key_3': 'value_3',\n",
    "    }\n",
    "\n",
    "for key, value in my_dict.items():\n",
    "    print('\\nKey: %s' % key)\n",
    "    print('Value: %s' % value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('key_1', 'value_1'), ('key_2', 'value_2'), ('key_3', 'value_3')])\n"
     ]
    }
   ],
   "source": [
    "my_dict = {'key_1': 'value_1',\n",
    "    'key_2': 'value_2',\n",
    "    'key_3': 'value_3',\n",
    "    }\n",
    "\n",
    "print(my_dict.items())\n",
    "\n",
    "# 上例中，遍历了这个元组列表，并且把每个元组的第一个和第二个item显示出来"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 遍历字典里所有的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Key: key_1\n",
      "Key: key_2\n",
      "Key: key_3\n"
     ]
    }
   ],
   "source": [
    "my_dict = {'key_1': 'value_1',\n",
    "    'key_2': 'value_2',\n",
    "    'key_3': 'value_3',\n",
    "    }\n",
    "\n",
    "for key in my_dict:\n",
    "    print('Key: %s' % key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The following Python words have been defined:\n",
      "- list\n",
      "- dictionary\n",
      "- function\n"
     ]
    }
   ],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "# Show the words that are currently in the dictionary.\n",
    "print(\"The following Python words have been defined:\")\n",
    "for word in python_words:\n",
    "    print(\"- %s\" % word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The following Python words have been defined:\n",
      "- list\n",
      "- dictionary\n",
      "- function\n",
      "\n",
      "What word would you like to learn about? list\n",
      "\n",
      "list: A collection of values that are not connected, but have an order.\n"
     ]
    }
   ],
   "source": [
    "###highlight=[12,13,14]\n",
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "# Show the words that are currently in the dictionary.\n",
    "print(\"The following Python words have been defined:\")\n",
    "for word in python_words:\n",
    "    print(\"- %s\" % word)\n",
    "    \n",
    "# Allow the user to choose a word, and then display the meaning for that word.\n",
    "requested_word = input(\"\\nWhat word would you like to learn about? \")\n",
    "print(\"\\n%s: %s\" % (requested_word, python_words[requested_word]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "**练习**\n",
    "\n",
    "在上面已经完成练习的基础上，设计一个“你想知道哪种食物的卡路里？”"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 遍历字典里所有的value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Value: value_1\n",
      "Value: value_2\n",
      "Value: value_3\n"
     ]
    }
   ],
   "source": [
    "my_dict = {'key_1': 'value_1',\n",
    "    'key_2': 'value_2',\n",
    "    'key_3': 'value_3',\n",
    "    }\n",
    "\n",
    "for value in my_dict.values():\n",
    "    print('Value: %s' % value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Meaning: A collection of values that are not connected, but have an order.\n",
      "Meaning: A collection of key-value pairs.\n",
      "Meaning: A named set of instructions that defines a set of actions in Python.\n"
     ]
    }
   ],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "for meaning in python_words.values():\n",
    "    print(\"Meaning: %s\" % meaning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "A collection of values that are not connected, but have an order.\n",
      "\n",
      "What word do you think this is?\n",
      "list  dictionary  function  \n",
      "- list\n",
      "You got it!\n",
      "\n",
      "A collection of key-value pairs.\n",
      "\n",
      "What word do you think this is?\n",
      "list  dictionary  function  \n",
      "- dictionary\n",
      "You got it!\n",
      "\n",
      "A named set of instructions that defines a set of actions in Python.\n",
      "\n",
      "What word do you think this is?\n",
      "list  dictionary  function  \n",
      "- function\n",
      "You got it!\n"
     ]
    }
   ],
   "source": [
    "python_words = {'list': 'A collection of values that are not connected, but have an order.',\n",
    "                'dictionary': 'A collection of key-value pairs.',\n",
    "                'function': 'A named set of instructions that defines a set of actions in Python.',\n",
    "                }\n",
    "\n",
    "def show_words(python_words):\n",
    "    # A simple function to show the words in the dictionary.\n",
    "    display_message = \"\"\n",
    "    for word in python_words.keys():\n",
    "        display_message += word + '  '\n",
    "    print(display_message)\n",
    "\n",
    "# Print each meaning, one at a time, and ask the user\n",
    "#  what word they think it is.\n",
    "for meaning in python_words.values():\n",
    "    print(\"\\n%s\" % meaning)\n",
    "\n",
    "    # Assume the guess is not correct; keep guessing until correct.\n",
    "    correct = False\n",
    "    while not correct:\n",
    "        \n",
    "        print(\"\\nWhat word do you think this is?\")\n",
    "        show_words(python_words)\n",
    "        guessed_word = input(\"- \")    \n",
    "        \n",
    "        # The guess is correct if the guessed word's meaning matches the current meaning.\n",
    "        if python_words[guessed_word] == meaning:\n",
    "            print(\"You got it!\")\n",
    "            correct = True\n",
    "        else:\n",
    "            print(\"Sorry, that's just not the right word.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**练习**  \n",
    "\n",
    "常见三种运动的消耗热量表：  \n",
    "慢走 (一小时4公里) 255 卡   \n",
    "慢跑 (一小时9公里) 655 卡   \n",
    "羽毛球（一小时） 440 卡   \n",
    "\n",
    "模仿上例做个刷题，直到答对而且答完为止。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 嵌套"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 字典里的字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "我知道每100g的牛奶:\n",
      "热量: 60千卡\n",
      "蛋白质: 6.1g\n",
      "脂肪: 3g\n",
      "\n",
      "我知道每100g的豆腐:\n",
      "热量: 82千卡\n",
      "蛋白质: 8.1g\n",
      "脂肪: 3.7g\n",
      "\n",
      "我知道每100g的草鱼:\n",
      "热量: 112千卡\n",
      "蛋白质: 16.6g\n",
      "脂肪: 5.2g\n"
     ]
    }
   ],
   "source": [
    "foods = {'牛奶': {'热量': '60千卡', '蛋白质': '6.1g', '脂肪':'3g'},\n",
    "        '豆腐': {'热量': '82千卡', '蛋白质': '8.1g', '脂肪':'3.7g'},\n",
    "        '草鱼': {'热量': '112千卡', '蛋白质': '16.6g', '脂肪':'5.2g'},\n",
    "        }\n",
    "\n",
    "\n",
    "for foods_name, foods_information in foods.items():\n",
    "    print(\"\\n我知道每100g的%s:\" % foods_name.title())\n",
    "    print(\"热量: \" + foods_information['热量'])\n",
    "    print(\"蛋白质: \" + foods_information['蛋白质'])\n",
    "    print(\"脂肪: \" + foods_information['脂肪'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 比较集合和字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "set_example1 = {'age':30}\n",
    "set_example2 = {'age', 30}  # 无序，可写\n",
    "print(type(set_example1))\n",
    "print(type(set_example2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 集合里的元素不能重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 1, 3]\n",
      "{1, 2, 3}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li = [1,2,1,3]             # 列表\n",
    "print(li)\n",
    "\n",
    "se = {1,2,1,3}             # 集合\n",
    "print(se)\n",
    "list(se)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
